<template>
	<view class="container">
	
		<view class="list-cell b-b m-t"  hover-class="cell-hover" @click="uploadavatar" >
			<text class="cell-tit">头像</text>
			<image class="avatar" :src="userInfo.avatar"></image>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell b-b m-t"  hover-class="cell-hover" @click="changemobile">
			<text class="cell-tit">手机号</text>
			<text class="cell-tip">{{userInfo.mobile}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
	<!-- 	<view class="list-cell b-b m-t"  hover-class="cell-hover" @click="bindwechat">

			<text class="cell-tit">绑定微信</text>
			<text class="cell-tip">{{isbind?'已绑定': '未绑定'}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view> -->
	 <view class="list-cell b-b m-t"  hover-class="cell-hover" @click="gochangenick">
	 	<text class="cell-tit">昵称</text>
	 	<text class="cell-tip">{{userInfo.nickname}}</text>
	 	<text class="cell-more yticon icon-you"></text>
	 </view>
 
		<view class="list-cell log-out-btn" @click="toLogout">
			<text class="cell-tit">退出登录</text>
		</view>
	</view>
</template>

<script>
 
	export default {
		data() {
			return {
				isbind:false,
				userInfo:{
					"mobile":"",
					"nickname":"",
					"avatar":"/static/missing-face.png",
				}
			};
		},
		onLoad() {
			this.getUserinfo();
		},
		onShow() {
			let that = this;
			uni.$on("changemobile",mobile=>{
				that.userInfo.mobile=mobile;
			});
			uni.$on("changenickname",nickname=>{
				that.userInfo.nickname=nickname;
			});
		},
		methods:{
		 
	     changemobile(){
		      uni.navigateTo({
		      	url:"/pages/user/changemobile?mobile="+this.userInfo.mobile
		      })
	     },
		 gochangenick(){
		      uni.navigateTo({
		      	url:"/pages/user/changenickname"
		      });
	     },
		 //绑定微信
		 bindwechat(){
			 let that = this;
			 uni.login({
			 	provider:"weixin",
			     	success(loginRes) {
			     		console.log(loginRes);
			 						let code = loginRes.code;
			 						that.$util.req("/user/loginByWeixin",{code:loginRes.code},false,function(res){
			 							//登录成功
			 							that.$util.setToken(res.data.token);
			 						    that.isbind = true;
			 						});
			     	},
			 					fail(e) {
			 						uni.showToast({
			 							title:"绑定失败"+e.errMsg
			 						});
			 						console.log("绑定失败");
			 						console.log(e);
			 					}
			 })
		 },
		  getUserinfo(){
		  	let that = this;
		  	this.$util.req("/user/getInfo",{},true,function(res){
			 
				that.isbind = res.data.bindwechat==1 ?true :false; 
		  		that.userInfo = res.data; 
		  	});
		  },
			uploadavatar(){
				let that = this;
				uni.chooseImage({
				    success: (chooseImageRes) => {
				        const tempFilePaths = chooseImageRes.tempFilePaths;
						let upurl = this.$util.baseurl+"/user/uploadavatar";
				         uni.uploadFile({
				            url: upurl, 
				            filePath: tempFilePaths[0],
				            name: 'file',
				            formData: {
				                'token': this.$util.getToken()
				            },
				            success: (uploadFileRes) => {
								let result = JSON.parse(uploadFileRes.data);
								 
								if(result.code!=1){
									uni.showToast({
										title:result.msg
									});
									return ;
								}
								that.userInfo.avatar = result.data.fullurl;
				            },
							fail:(e)=>{	
								console.log(e);
							}
				        });

				    }
				});
			},
		
			//退出登录
			toLogout(){
				console.log("跳转登录");
				let that = this;
			
			 
				uni.showModal({
				    content: '确定要退出登录么',
				    success: (e)=>{
				    	if(e.confirm){
				    		//清除登录信息
							that.$util.logout();
				    		setTimeout(()=>{//跳转
				    			uni.switchTab({
				    				url:"/pages/index/index"
				    			});
				    		}, 200)
				    	}
				    }
				});
			},
	 

		}
	}
</script>

<style lang='scss'>
	page{
		background: $page-color-base;
	}
	.list-cell{
		display:flex;
		align-items:center;
		padding: 20upx $page-row-spacing;
		line-height:60upx;
		position:relative;
		background: #fff;
		.avatar{
		 
			height: 50upx;
			width: 50upx;
		}
		justify-content: center;
		&.log-out-btn{
			margin-top: 40upx;
			.cell-tit{
				color: $uni-color-primary;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover{
			background:#fafafa;
		}
		&.b-b:after{
			left: 30upx;
		}
		&.m-t{
			margin-top: 16upx; 
		}
		.cell-more{
			align-self: baseline;
			font-size:$font-lg;
			color:$font-color-light;
			margin-left:10upx;
		}
		.cell-tit{
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right:10upx;
		}
		.cell-tip{
			font-size: $font-base;
			color: $font-color-light;
		}
		switch{
			transform: translateX(16upx) scale(.84);
		}
	}
</style>
